$(function() {

	var defaultPagingObj = {
		total: 20,
		pageSize: 10,
		page: 1
	};

	PAGE_DATA.pagingObj = $.extend(defaultPagingObj, PAGE_DATA.pagingObj);
	// 主要分类：咖啡豆及咖啡相关,等.
	commTemplUtil.initPage();

	// 渲染左侧菜单;
	commTemplUtil.initCommLeftMemu();
	//左边导航选中
	commTemplUtil.setLeftMenu(3);
	//初始化获取列表
	initialize(1);

	//点击搜索
	search_btn();

	// 时间-jquery-UI.
	$(".sel-order-time-1").datepicker();
	$(".sel-order-time-2").datepicker();



});

function initialize(page) {
	var onClickObj = {
		onClick: function() {
			var clickPage = $(this).attr("data-page");
			initialize(clickPage);
		}
	};

	var tmplObj = _.template($('#item-template').html());
	var time1 = $(".sel-order-time-1").val() || "";
    var time2 = $(".sel-order-time-2").val() || "";
    time(time1,time2);
     var state = $("#oper_state  option:selected").val() || "";
	$.ajax({
		url: PAGE_CONFIG.requestUrl.counterenquiry,
		type: "get",
		dataType: "json",
		data: {
			page: page + "",
			time1: time1,
            time2:time2,
            state: state,
			pageSize: PAGE_DATA.pagingObj.pageSize,
		},
		success: function(result) {
			var datas = $.extend([], result.data);
			var tmplObjHtml = tmplObj(datas);
			$("#commTabs_1_body").html(tmplObjHtml);
			template1();
			counterOfferDel();
			var tmpConf = $.extend(result.pagingConfData, onClickObj)
			commTemplUtil.initPaging("#comm-paging", tmpConf);
		},
		error: function(xhr, status, error) {
			var tmplObjHtml = tmplObj([]);
			$("#commTabs_1_body").html(tmplObjHtml);
		}
	});
};

function search_btn() {
	$(".sel-search-btn").click(function() {
		initialize(1);
	});
};
//弹出框的事件方法
function template1() {
	var tmplObj1 = _.template($('#item-template1').html());
	var tmplObj = _.template($('#item-template').html());
	//弹出框
	$(".handle").click(function() {
		var getId = $(this).attr("code");
		var dia = dialog({
			title: '   ',
			content: tmplObj1
		});
		dia.showModal();
		$(".submits").attr("code", getId);
		$(".submits").click(function() {
			var thisId = $(this).attr("code");
			var time1 = $(".sel-order-time-1").val() || "";
    		var time2 = $(".sel-order-time-2").val() || "";
    		time(time1,time2);
     		var state = $("#oper_state  option:selected").val() || "";
     		var currentPage = $("#comm-paging").attr("current-page");
			$.ajax({
				url: PAGE_CONFIG.requestUrl.counterenquiry1,
				type: "put",
				dataType: "json",
				data: {
					id: getId,
					time1: time1,
            		time2:time2,
            		state: state,
            		page: currentPage,
                    pageSize: PAGE_DATA.pagingObj.pageSize,
					_token: COMM_DATA.csrfToken,
				},
				success: function(result) {
					datas = $.extend([], result.data);
					var tmplObjHtml = tmplObj(datas);
					$("#commTabs_1_body").html(tmplObjHtml);
					dia.close();
					initialize(1);
				},
				error: function(xhr, status, error) {
					var tmplObjHtml = tmplObj([]);
					$("#commTabs_1_body").html(tmplObjHtml);
				}
			});
		});
		$(".remove").click(function() {
			dia.close();
		});
	});
};

//删除的事件
function counterOfferDel() {
	var tmplObj = _.template($('#item-template').html());
	$(".sel-edit-del").click(function() {
		var dataId = $(this).parents("ul").attr("data-id");
		var currentPage = $("#comm-paging").attr("current-page");

		var deleDialog = dialog({
			id: "del-classify-dialog",
			width: 300,
			height: 100,
			title: "注意！",
			content: '确认删除此条数据？',
			ok: doDele,
			cancel: function() {},
			cancelValue: "取消",
			okValue: "确定",
		});

		deleDialog.showModal();
		//删除弹出框的事件
		function doDele() {
			var time1 = $(".sel-order-time-1").val() || "";
    		var time2 = $(".sel-order-time-2").val() || "";
    		time(time1,time2);
     		var state = $("#oper_state  option:selected").val() || "";
     		var currentPage = $("#comm-paging").attr("current-page");
			$.ajax({
				url: PAGE_CONFIG.requestUrl.counterenquiry2,
				type: "delete",
				data: {
					time1: time1,
            		time2:time2,
            		state: state,
					id: dataId,
					page: currentPage,
                    pageSize: PAGE_DATA.pagingObj.pageSize,
					_token: COMM_DATA.csrfToken
				},
				success: function(result) {
					var datas = $.extend([], result.data);
					var tmplObjHtml = tmplObj(datas);
					$("#commTabs_1_body").html(tmplObjHtml);
					initialize(1);
				},
				error: function() {

					var deleFailDialog = dialog({
						id: "del-classify-dialog",
						content: '删除失败。。。',
						ok: function() {},
						okValue: "确定",
					});
					deleFailDialog.showModal();
				}
			});
		};

		return false;
	});
};
function time(time1,time2) {
    
    if (!(time1 == "" || time2 == "")) {
        // 只有输入两个时间 才需要判断 时间合法性.
        var chkFlag = commTemplUtil.checkDate(time1, time2);

        if (!chkFlag) {
            // 不合法--date1 > date2
            commTemplUtil.showErrorDialog({
                title: "注意",
                message: "请检查时间先后顺序!",
                ok_txt: "确定"
            });
            return false;
        }
    }
}